﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class LogChuyenGoldDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Log_ChuyenGold table.
		/// </summary>
		public virtual int Insert(LogChuyenGoldInfo logChuyenGoldInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CreateDate", logChuyenGoldInfo.CreateDate),
				new SqlParameter("@IP", logChuyenGoldInfo.IP),
				new SqlParameter("@EmailTransfer", logChuyenGoldInfo.EmailTransfer),
				new SqlParameter("@EmailGet", logChuyenGoldInfo.EmailGet),
				new SqlParameter("@Gold", logChuyenGoldInfo.Gold)
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_Insert", parameters);
		}


        public virtual int Insert1(LogChuyenGoldInfo logChuyenGoldInfo)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CreateDate", logChuyenGoldInfo.CreateDate),
				new SqlParameter("@IP", logChuyenGoldInfo.IP),
				new SqlParameter("@EmailTransfer", logChuyenGoldInfo.EmailTransfer),
				new SqlParameter("@EmailGet", logChuyenGoldInfo.EmailGet),
				new SqlParameter("@Gold", logChuyenGoldInfo.Gold),
                new SqlParameter("@Description", logChuyenGoldInfo.Description),
                new SqlParameter("@CodeMember_From", logChuyenGoldInfo.CodeMember_From),
                new SqlParameter("@CodeMember_To", logChuyenGoldInfo.CodeMember_To)
           

                	
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_Insert1", parameters);
        }

		/// <summary>
		/// Updates a record in the Log_ChuyenGold table.
		/// </summary>
		public virtual void Update(LogChuyenGoldInfo logChuyenGoldInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logChuyenGoldInfo.LogId),
				new SqlParameter("@CreateDate", logChuyenGoldInfo.CreateDate),
				new SqlParameter("@IP", logChuyenGoldInfo.IP),
				new SqlParameter("@EmailTransfer", logChuyenGoldInfo.EmailTransfer),
				new SqlParameter("@EmailGet", logChuyenGoldInfo.EmailGet),
				new SqlParameter("@Gold", logChuyenGoldInfo.Gold)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the Log_ChuyenGold table by its primary key.
		/// </summary>
		public virtual void Delete(int logId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Log_ChuyenGold table.
		/// </summary>
		public virtual LogChuyenGoldInfo Select(int logId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeLogChuyenGoldInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

        /// <summary>
        /// Lịch sử giao dịch
        /// </summary>
        public DataTable SelectTransactionHistory(string email)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@Email",email)
                                            
		};
            using (DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_TransactionHistory", parameters))
            {
                if (dt.Rows.Count > 0)
                    return dt;
                else
                {
                    return null;
                }
            }
        }

        #region Lấy thông tin của từng bản ghi

        public virtual LogChuyenGoldInfo SelectInfo(string param,int flag)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Param",param),
                new SqlParameter("@Flag",flag)
               
			};

            using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_SelectInfo", parameters))
            {
                if (dataReader.Read())
                {
                    return MakeLogChuyenGoldInfo(dataReader);
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion

        //public CHRTList<LogChuyenGoldInfo> SelectAll(string param,int flag)
        //{
        //    SqlParameter[] parameters = new SqlParameter[]
        //                                  {
        //                                      new SqlParameter("@Param",param),
        //                                       new SqlParameter("@Flag",flag)

        //};
        //    using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_SelectInfo", parameters))
        //    {
        //        CHRTList<LogChuyenGoldInfo> logChuyenGoldList = new CHRTList<LogChuyenGoldInfo>();
        //        while (dataReader.Read())
        //        {
        //            LogChuyenGoldInfo logChuyenGoldInfo = MakeLogChuyenGoldInfo(dataReader);
        //            logChuyenGoldList.Add(logChuyenGoldInfo);
        //        }

        //        return logChuyenGoldList;
        //    }
        //}
        //phân trang lịch sử giao dịch
        public DataSet ListTransactionHistory(string email, int currPage, int recodperPage, int pageSize,string link)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {                                              
                                             
                                              new SqlParameter("@currPage",currPage),
                                              new SqlParameter("@recodperpage",recodperPage),
                                              new SqlParameter("@Pagesize",pageSize),
                                              new SqlParameter("@Email",email),
                                              new SqlParameter("@link",link)

                                             
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "LogChuyenGold_PagingByLogID", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }
        #region update Status


        #region cập nhật lại trạng thái
        public virtual void UpdateStatus(int logId, int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@LogId", logId),
                new SqlParameter("@Status", status),
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_UpdateStatus", parameters);
        }

        #endregion
        #endregion

        /// <summary>
		/// Selects all records from the Log_ChuyenGold table.
		/// </summary>
		public CHRTList<LogChuyenGoldInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_SelectAll"))
			{
				CHRTList<LogChuyenGoldInfo> logChuyenGoldInfoList = new CHRTList<LogChuyenGoldInfo>();
				while (dataReader.Read())
				{
					LogChuyenGoldInfo logChuyenGoldInfo = MakeLogChuyenGoldInfo(dataReader);
					logChuyenGoldInfoList.Add(logChuyenGoldInfo);
				}

				return logChuyenGoldInfoList;
			}
		}

        public int GetCount(int Id, DateTime createOnFrom, DateTime createOnTo)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@Id", Id), 
                                                new SqlParameter("@CreateOnFrom",createOnFrom),
                                                new SqlParameter("@CreateOnTo",createOnTo)                                               
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_GetCount", parameters);

        }

        public virtual DataTable Search(int status, string starDatetime, string endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@Status", status),
                new SqlParameter("@starDatetime", starDatetime),
                new SqlParameter("@endDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", 0)
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Log_Chuyegold_SearchNew", parameters);
        }

        public DataTable SearchAll(DateTime createdOnFrom, DateTime createdOnTo, int from, int to)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@CreatedOnFrom",createdOnFrom),
                                              new SqlParameter("@CreatedOnTo",createdOnTo),
                                              new SqlParameter("@From",from),
                                              new SqlParameter("@To",to)
		};
            using (DataTable dt = SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_SearchAll", parameters))
            {
                if (dt.Rows.Count > 0)
                    return dt;
                else
                {
                    return null;
                }
            }
        }


        public DataSet ListLogChuyenGoldPage(int logId, DateTime createOnFrom, DateTime createOnTo, int currPage, int recodperPage, int pageSize, int total, string link)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@LogId", logId),
                                              new SqlParameter("@CreatedOnFrom",createOnFrom),
                                              new SqlParameter("@CreatedOnTo",createOnTo),
                                              new SqlParameter("@currPage", currPage),
                                              new SqlParameter("@recodperPage", recodperPage),
                                              new SqlParameter("@pageSize", pageSize), 
                                              new SqlParameter("@total", total),
                                              new SqlParameter("@link", link)
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "Log_ChuyenGold_search_Page", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }

        public virtual int SelectCount(bool isTotalRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
				new SqlParameter("@isTotalRow", true.ToString())
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "Sp_LogChuyenGold_Search", parameters);
        }

        public virtual DataTable Search(int beginRow, int endRow, string orderByColumn)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
				new SqlParameter("@isTotalRow", false.ToString())
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "Sp_LogChuyenGold_Search", parameters);
        }

		/// <summary>
		/// Creates a new instance of the Log_ChuyenGold class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual LogChuyenGoldInfo MakeLogChuyenGoldInfo(SqlDataReader dataReader)
		{
			LogChuyenGoldInfo logChuyenGoldInfo = new LogChuyenGoldInfo();
			logChuyenGoldInfo.LogId = SqlClientUtility.GetInt32(dataReader,DbConstants.LOG_CHUYENGOLD.LOGID, 0);
			logChuyenGoldInfo.CreateDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.LOG_CHUYENGOLD.CREATEDATE, DateTime.Now);
			logChuyenGoldInfo.IP = SqlClientUtility.GetString(dataReader,DbConstants.LOG_CHUYENGOLD.IP, String.Empty);
			logChuyenGoldInfo.EmailTransfer = SqlClientUtility.GetString(dataReader,DbConstants.LOG_CHUYENGOLD.EMAILTRANSFER, String.Empty);
			logChuyenGoldInfo.EmailGet = SqlClientUtility.GetString(dataReader,DbConstants.LOG_CHUYENGOLD.EMAILGET, String.Empty);
			logChuyenGoldInfo.Gold = SqlClientUtility.GetFloat(dataReader,DbConstants.LOG_CHUYENGOLD.GOLD, 0.0F);
         //logChuyenGoldInfo.Description = SqlClientUtility.GetString(dataReader, DbConstants.LOG_CHUYENGOLD.d,String.Empty);

			return logChuyenGoldInfo;
		}

		#endregion
	}
}
